package database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import mockup.Mockup;
import mockup.RoomMockup;

import server.database.ConnectionDatabase;

/**
 * Done men mangler javadoc
 * @author daniel
 */
public class RoomDB implements DatabaseAction {

	private String query;
	private Statement st;
	
	public RoomDB() {
		try {
			st = ConnectionDatabase.getConnection().createStatement();			
		} catch (Exception e) {
			System.err.println("AppoitmentDB::Constructor:Exception: " + e.getMessage());
		}
	}

	@Override
	public RoomMockup get(int id) {
		query = "select * FROM " + ConnectionDatabase.getNameDB() + ".room " +
		"where room_id = " + id;
		
		ResultSet rs = ConnectionDatabase.get(query, st);
		try {
			while (rs.next()) {
				int room_id = rs.getInt("room_id");
				String name = rs.getString("name");
				String casualName = rs.getString("casual_name");
				String location = rs.getString("location");
				int capacity = rs.getInt("capacity");
				String description = rs.getString("description");
				RoomMockup room = new RoomMockup(room_id, name, location, casualName, description, capacity);
				return room;
			}
		} catch (SQLException e ) {
			System.err.println("RoomDB::get:SQLException: " + e.getMessage());
		}
		return null;
	}

	@Override
	public int insert(Mockup m) {
		int dbID = -1;
		RoomMockup room = (RoomMockup)m;
		if(0 > room.getId()) {
			query = "INSERT INTO " + ConnectionDatabase.getNameDB()+".room (" +
			"name, casual_name, location, capacity, description) " +
			" values ('" + room.getName() + "','" +
			room.getCasualName() + "','" +
			room.getLocation() + "'," +
			room.getCapacity() + ",'" + 
			room.getDescription() + "')";

			dbID = ConnectionDatabase.insert(query);
		} else {
			update(m);
		}
		return dbID;
	}

	@Override
	public void delete(Mockup m) {
		RoomMockup room = (RoomMockup)m;
		if(room.getId() > -1) {
			query = "DELETE FROM " + ConnectionDatabase.getNameDB() + ".room " +
			"where room_id = " + room.getId();
			
			ConnectionDatabase.update(query, st);	
		}
	}


	@Override
	public void update(Mockup m) {
		RoomMockup room = (RoomMockup)m;
		if(room.getId() > -1) {
			query = "UPDATE " + ConnectionDatabase.getNameDB()+".room SET " +
			"name = '" + room.getName() + "'," +
			"casual_name = '" + room.getCasualName() + "'," +
			"location = '" + room.getLocation() + "'," +
			"capacity = " + room.getCapacity() + "," + 
			"description = '" + room.getDescription() + "' " +
			"where room_id = " + room.getId();
			
			ConnectionDatabase.update(query, st);
			
		} else {
			System.err.println("Rom har ikke id; bruk insert i stedet");
		}
	}
	
	public List<RoomMockup> getAll() {
		List<RoomMockup> rooms = new ArrayList<RoomMockup>();
		query = "select * FROM " + ConnectionDatabase.getNameDB() + ".room";
		ResultSet rs = ConnectionDatabase.get(query, st);
		try {
			while (rs.next()) {
				int room_id = rs.getInt("room_id");
				String name = rs.getString("name");
				String casualName = rs.getString("casual_name");
				String location = rs.getString("location");
				int capacity = rs.getInt("capacity");
				String description = rs.getString("description");
				RoomMockup room = new RoomMockup(room_id, name, location, casualName, description, capacity);
				rooms.add(room);
			}
		} catch (SQLException e ) {
			System.err.println("RoomDB::getAll:SQLException: " + e.getMessage());
		}
		return rooms;
	}
	
	@Override
	public void emptyTable() {
		query = "TRUNCATE TABLE " + ConnectionDatabase.getNameDB() + ".room";
		ConnectionDatabase.update(query, st);
	}
}
